#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <string.h>

#include "char_driver.h"

int main(int argc, char const *argv[])
{
    if (argc < 2){
        printf("errno=%d\n",errno);
        fprintf(stderr,"[file:%s] [func:%s] [line:%d]\n",__FILE__,__func__,__LINE__);
        return -1;
    }

    int fd = open(argv[1], O_RDWR);
    if(fd < 0){
        printf("errno=%d\n",errno);
        fprintf(stderr,"[file:%s] [func:%s] [line:%d]\n",__FILE__,__func__,__LINE__);
        return -1;
    }

    int len = -1;
    ioctl(fd, GET_BUF_LEN, &len);
    printf("GET_BUF_LEN=%d\n",len);

    write(fd, "hello", sizeof("hello"));
    ioctl(fd, GET_CUR_LEN, &len);
    printf("[write]GET_CUR_LEN=%d\n",len);
    
    char buf[3]={0};
    read(fd, buf, 3);
    ioctl(fd, GET_CUR_LEN, &len);
    printf("[read]buf=%s\n",buf);
    printf("[read]GET_CUR_LEN=%d\n",len);

    ioctl(fd, CLEAR_BUF);
    ioctl(fd, GET_CUR_LEN, &len);
    printf("CLEAR_BUF=%d\n",len);

    memset(buf,0,sizeof(buf));
    read(fd, buf, 3);
    printf("[read2]buf=%s\n",buf);

    close(fd);
    return 0;
}

